package com.pm.service.dept.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pm.entity.po.dept.DepartmentEvaluate;
import com.pm.entity.po.dept.DepartmentEvaluatePo;
import com.pm.entity.po.security.User;
import com.pm.mapper.dept.DepartmentEvaluateMapper;
import com.pm.service.dept.DepartmentEvaluateService;
import com.pm.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: pm_info_api
 * @description: 成员单位评价
 * @author: xzj
 * @create: 2020-08-19 11:24
 **/
@Service
public class DepartmentEvaluateServiceImpl extends BaseServiceImpl<DepartmentEvaluate> implements DepartmentEvaluateService {
    @Autowired
    DepartmentEvaluateMapper departmentEvaluateMapper;

    @Resource
    private void setBaseMapper(DepartmentEvaluateMapper departmentEvaluateMapper) {
        super.setBaseMapper(departmentEvaluateMapper);
    }

    @Override
    public List<DepartmentEvaluate> getListOne(DepartmentEvaluate departmentEvaluate) {
        return departmentEvaluateMapper.getListOne(departmentEvaluate);
    }

    @Override
    public Integer updateDepartmentEvaluate(DepartmentEvaluate departmentEvaluate) {
        return departmentEvaluateMapper.updateDepartmentEvaluate(departmentEvaluate);
    }


    @Override
    public Integer addOne(JSONObject jsonObject, User user) {
        DepartmentEvaluatePo departmentEvaluatePo = new DepartmentEvaluatePo();
        departmentEvaluatePo.setDepartmentId(jsonObject.getInteger("departmentId"));
        departmentEvaluatePo.setTimeNode(jsonObject.getInteger("timeNode"));
        departmentEvaluatePo.setYear(jsonObject.getString("year"));
        // 分数
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        // 每一项得分数据
        JSONObject arrayData = null;
        DepartmentEvaluate departmentEvaluate = new DepartmentEvaluate();
        for (int i = 0, j = jsonArray.size(); i < j; i++) {
            arrayData = jsonArray.getJSONObject(i);
            //departmentEvaluate = new DepartmentEvaluate();
            departmentEvaluate.setDepartmentId(departmentEvaluatePo.getDepartmentId());
            departmentEvaluate.setTimeNode(departmentEvaluatePo.getTimeNode());
            departmentEvaluate.setYear(departmentEvaluatePo.getYear());
            departmentEvaluate.setAddUser(user.getUserId());
            departmentEvaluate.setEditUser(user.getUserId());
            departmentEvaluate.setTaskId(arrayData.getInteger("taskId"));
            departmentEvaluate.setScore(arrayData.getDouble("score"));
            departmentEvaluateMapper.addOne(departmentEvaluate);
        }
        return 1;
    }

    @Override
    public Integer removeOne(Integer departmentId) {
        return departmentEvaluateMapper.removeOne(departmentId);
    }
}
